﻿using dz.u9.jky.schema.Common;
using dz.u9.jky.schema.Customer;
using System.Collections.Generic;
using UFIDA.U9.CBO.Pub.Controller;
using UFIDA.U9.ISV.Customer;

namespace dz.inner.jky.api.service
{
    internal class CustomerService : BaseService
    {
        public U9CResult Add(DzCustomerDto dto)
        {
            return util.U9Invoke(__Add, dto);
        }

        //public void Modify1(DzCustomerDto dto)
        //{
        //    List<long> orgs = util.GetOrgs(dto.Org);

        //    foreach (long id in orgs)
        //    {
        //        Organization org = Organization.Finder.FindByID(id);

        //        Customer customer = Customer.FindByCode(org, dto.Code);

        //        if (customer == null) continue;

        //        using (ISession session = Session.Open())
        //        {
        //            if (dto.Effective != null && dto.Effective.EffectiveDate != customer.Effective.EffectiveDate)
        //                customer.Effective.EffectiveDate = dto.Effective.EffectiveDate;

        //            if (dto.Effective != null && dto.Effective.DisableDate != customer.Effective.DisableDate)
        //                customer.Effective.DisableDate = dto.Effective.DisableDate;

        //            if (customer.Name != dto.Name && !string.IsNullOrEmpty(dto.Name))
        //                customer.Name = dto.Name;

        //            if (customer.CustomerCategory.Code != dto.CustomerCategory && !string.IsNullOrEmpty(dto.CustomerCategory))
        //                customer.CustomerCategory = CustomerCategory.Finder.Find($"code = '{dto.CustomerCategory}' and org = {id}");

        //            if (customer.StateTaxNo != dto.StateTaxNo && !string.IsNullOrEmpty(dto.StateTaxNo))
        //                customer.StateTaxNo = dto.StateTaxNo;

        //            if (customer.ShippmentRule.Code != dto.ShippmentRule && !string.IsNullOrEmpty(dto.ShippmentRule))
        //                customer.ShippmentRule = ShipmentBy.Finder.Find($"code = '{dto.ShippmentRule}' and org = {id}");

        //            if (customer.ARConfirmTerm.Code != dto.ARConfirmTerm && !string.IsNullOrEmpty(dto.ARConfirmTerm))
        //                customer.ARConfirmTerm = ARAccrueTerm.Finder.Find($"code = '{dto.ARConfirmTerm}' and org = {id}");

        //            if (customer.RecervalTerm.Code != dto.RecervalTerm && !string.IsNullOrEmpty(dto.RecervalTerm))
        //                customer.RecervalTerm = RecTerm.Finder.Find($"code = '{dto.RecervalTerm}' and org = {id}");

        //            if (customer.TaxSchedule.Code != dto.TaxSchedule && !string.IsNullOrEmpty(dto.TaxSchedule))
        //                customer.TaxSchedule = TaxSchedule.Finder.Find($"code = '{dto.TaxSchedule}' and org = {id}");

        //            if (customer.TradeCurrency.Code != dto.TradeCurrency && !string.IsNullOrEmpty(dto.TradeCurrency))
        //                customer.TradeCurrency = Currency.FindByCode(dto.TradeCurrency);

        //            if (customer.PayCurrency.Code != dto.PayCurrency && !string.IsNullOrEmpty(dto.PayCurrency))
        //                customer.PayCurrency = Currency.FindByCode(dto.TradeCurrency);

        //            if (customer.DescFlexField.PrivateDescSeg1 != dto.DescFlexField.PrivateDescSeg1 && dto.DescFlexField.PrivateDescSeg1 != null)
        //                customer.DescFlexField.PrivateDescSeg1 = dto.DescFlexField.PrivateDescSeg1;

        //            if (customer.DescFlexField.PrivateDescSeg2 != dto.DescFlexField.PrivateDescSeg2 && dto.DescFlexField.PrivateDescSeg2 != null)
        //                customer.DescFlexField.PrivateDescSeg2 = dto.DescFlexField.PrivateDescSeg2;

        //            if (customer.DescFlexField.PrivateDescSeg3 != dto.DescFlexField.PrivateDescSeg3 && dto.DescFlexField.PrivateDescSeg3 != null)
        //                customer.DescFlexField.PrivateDescSeg3 = dto.DescFlexField.PrivateDescSeg3;

        //            if (customer.DescFlexField.PrivateDescSeg4 != dto.DescFlexField.PrivateDescSeg4 && dto.DescFlexField.PrivateDescSeg4 != null)
        //                customer.DescFlexField.PrivateDescSeg4 = dto.DescFlexField.PrivateDescSeg4;

        //            if (customer.DescFlexField.PrivateDescSeg5 != dto.DescFlexField.PrivateDescSeg5 && dto.DescFlexField.PrivateDescSeg5 != null)
        //                customer.DescFlexField.PrivateDescSeg5 = dto.DescFlexField.PrivateDescSeg5;

        //            if (customer.DescFlexField.PrivateDescSeg6 != dto.DescFlexField.PrivateDescSeg6 && dto.DescFlexField.PrivateDescSeg6 != null)
        //                customer.DescFlexField.PrivateDescSeg6 = dto.DescFlexField.PrivateDescSeg6;

        //            if (customer.DescFlexField.PrivateDescSeg7 != dto.DescFlexField.PrivateDescSeg7 && dto.DescFlexField.PrivateDescSeg7 != null)
        //                customer.DescFlexField.PrivateDescSeg7 = dto.DescFlexField.PrivateDescSeg7;

        //            if (customer.DescFlexField.PrivateDescSeg8 != dto.DescFlexField.PrivateDescSeg8 && dto.DescFlexField.PrivateDescSeg8 != null)
        //                customer.DescFlexField.PrivateDescSeg8 = dto.DescFlexField.PrivateDescSeg8;

        //            if (customer.DescFlexField.PrivateDescSeg9 != dto.DescFlexField.PrivateDescSeg9 && dto.DescFlexField.PrivateDescSeg9 != null)
        //                customer.DescFlexField.PrivateDescSeg9 = dto.DescFlexField.PrivateDescSeg9;


        //            session.Commit();
        //        }
        //    }
        //}

        public void Modify(DzCustomerDto dto)
        {
            BatchModifyCustomerByDTOSV sv = new BatchModifyCustomerByDTOSV();

            sv.CustomerDTOs = Convert(dto, Query(dto));

            sv.Do();

        }
        private U9CResult __Add(DzCustomerDto dto)
        {
            U9CResult result = new U9CResult();

            BatchCreateCustomerByDTOSV svc = new BatchCreateCustomerByDTOSV();

            svc.CustomerDTOs = Convert(dto);

           var customers = svc.Do();

            result.IsSuccess = true;
            result.DocNo = customers[0].Code;
            result.Id = customers[0].ID;

            if (result.IsSuccess)
            {
                DocIssueService issueService = new DocIssueService();

                DocIssue issue = new DocIssue();
                issue.FromOrg = dto.Org;
                issue.EntityName = "UFIDA.U9.CBO.SCM.Customer.Customer";
                issue.Ids = new List<long>() { result.Id };

                issueService.Issued(issue);
            }
            return result;
        }

        private List<CustomerDTO> Convert(DzCustomerDto customer)
        {
            CustomerDTO dto = new CustomerDTO();

            ObjectCopyUtil util = new ObjectCopyUtil();

            util.CopyProperty(customer, ref dto);


            List<CustomerDTO> rtn = new List<CustomerDTO>() { dto };

            return rtn;
        }

        private List<CustomerDTO> Convert(DzCustomerDto customer, List<CustomerDTO> target)
        {           

            ObjectCopyUtil util = new ObjectCopyUtil();

            List<CustomerDTO> rtn = new List<CustomerDTO>();

            foreach (var item in target)
            {
                CustomerDTO dto = item;
                util.CopyProperty(customer, ref dto);

                rtn.Add(dto);
            }
            return rtn;
        }

        private List<CustomerDTO> Query(DzCustomerDto customer)
        {
            List<CustomerDTO> rtn = new List<CustomerDTO>();

            List<string> orgs = util.GetOrgCodes(customer.Org);

            List<QueryCustomerDTO> querys = new List<QueryCustomerDTO>();    

            foreach (var org in orgs)
            {
                QueryCustomerDTO parm = new QueryCustomerDTO();

                parm.Org = new CommonArchiveDataDTO() { Code = org };
                parm.Customer = new CommonArchiveDataDTO() { Code = customer.Code }; 

                querys.Add(parm);
            }

            BatchQueryCustomerByDTOSRV sv = new BatchQueryCustomerByDTOSRV();
            sv.QueryCustomerDTOs = querys;

            return sv.Do();
        }


    }
}
